package cn.com.scau.tstjxt.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.scau.tstjxt.action.TuserAction;
import cn.com.scau.tstjxt.dao.Tuser;


public class LoginServlet  extends HttpServlet  {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
	String path = "http://" + request.getServerName() + ":"+ request.getServerPort() + request.getContextPath();
	request.setCharacterEncoding("GBK");
	response.setContentType("text/html;charset=GBK");
	String action = request.getParameter("action");
	if("logout".equals(action)){
		request.getSession().removeAttribute("user");
		path=path+"/homepage/index.jsp";
		response.sendRedirect(path);
	}else {
		this.doPost(request, response);
	}
    
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
	String result ="fail";
	String path = "http://" + request.getServerName() + ":"
	+ request.getServerPort() + request.getContextPath();
	request.setCharacterEncoding("GBK");
	response.setContentType("text/html;charset=GBK");
	TuserAction tuseraction=TuserAction.getInstance();
	String id=request.getParameter("username");
	String password=request.getParameter("password");
	String profession=request.getParameter("profession");
	String sex=request.getParameter("sex");
	String insterest=request.getParameter("role");
	Tuser user = tuseraction.findbyname(id);
	System.out.println(user.getUserid());
	System.out.println(user.getPassword());
	if(user.getUserid().equals(id)&&user.getPassword().equals(password)){
	  result="sussess";	
	}
	if(result.equals("sussess")){
		request.getSession().setAttribute("user", user);
		path=path+"/homepage/index.jsp";
		response.sendRedirect(path);	
	}
	else{
		path=path+"/homepage/Login.jsp";
		response.sendRedirect(path);	
	}
	}
	
}


